
/**
 * 取页面DOM对象
 * @param id
 * @returns
 */
function $(id) { return document.getElementById(id);};

/**
 * AJAX函数
 */
var __ajax = function(url, cfg) {
    var args = arguments;
    if (args.length==0) return;

    var uri = undefined; //请求地址
    var data = undefined; //发送到服务器的数据
    var async = true; //true-异步;  false（同步）
    var complete = function(XHR, TS) {} //请求完成后回调函数 (请求成功或失败之后均调用)。
    var contentType = 'application/x-www-form-urlencoded';
    var context = undefined;
    var dataType = undefined;
    var success = function(data, status, XHR) {} //请求成功后的回调函数。
    var error = function(status, XHR) {} //请求失败后的回调函数。
    var timeout = 10000;
    var type = 'GET';

    var init = function(config) {
        uri = !!config.url ? config.url: uri;
        if (!!config.data && Object.prototype.toString.call(config.data)=='[object Object]') {
            var parameter = '';
            for (var k in config.data) {
                parameter = parameter + k +'='+ config.data[k] +'&';
            }
            data = parameter.substr(0, parameter.length-1);
        } else if (!!config.data && Object.prototype.toString.call(config.data)=='[object String]') {
            data = config.data;
        } else {}
        async = config.async!=undefined ? !!config.async: async;
        if (Object.prototype.toString.call(config.complete)=='[object Function]') {
            complete = eval(config.complete);
        }
        contentType = !!config.contentType ? config.contentType: contentType;
        context = !!config.context ? config.context: context;
        dataType = !!config.dataType ? config.dataType: dataType;
        if (Object.prototype.toString.call(config.success)=='[object Function]') {
            success = eval(config.success);
        }
        if (Object.prototype.toString.call(config.error)=='[object Function]') {
            error = eval(config.error);
        }
        timeout = !!config.timeout ? config.timeout: timeout;
        type = config.type!= undefined && config.type.toLowerCase()=='post' ? 'POST': type;

    };

    if (args.length==1 && Object.prototype.toString.call(arguments[0])=='[object String]') {
        uri = arguments[0];
    } else if (args.length==1 && Object.prototype.toString.call(arguments[0])=='[object Object]') {
        init(arguments[0]);
    } else if (args.length==2 && Object.prototype.toString.call(arguments[0])=='[object String]' && Object.prototype.toString.call(arguments[1])=='[object Function]') {
        uri = arguments[0];
        success = arguments[1];
    } else {
        init(arguments[1]);
        uri = arguments[0];
    };

    if (!uri) { return;}

    var result = {'context':null, 'data':null};
    var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : null);
    if (null!=xhr) {
        if (type.toLowerCase()=='get' && data!=undefined) {
            uri = uri.indexOf('?')==-1 ? uri +'?'+ data : uri +'&'+ data;
        }
        xhr.open(type.toUpperCase(), uri.indexOf('?')==-1 ? uri +'?ver='+ Math.random() : uri +'&ver='+ Math.random(), async);
        if (type.toLowerCase()=='post') {
            xhr.setRequestHeader("Content-type", contentType); //"application/x-www-form-urlencoded"
        }
        var ajaxTimeId = undefined;
        ajaxTimeId = setTimeout(function() { xhr.abort();}, timeout);
        xhr.onreadystatechange = function() {
            if (xhr.readyState==4) { //请求已完成，且响应已就绪
                var xhrRespText = xhr.responseText;
                if (xhrRespText!=null) { xhrRespText = xhrRespText.replace(/^\s*|\s*$/g, '');}
                if (dataType!= undefined && dataType.toLowerCase()=='json') { xhrRespText = eval('('+ xhrRespText +')');}; //xhrRespText = (new Function('','return '+ xhrRespText))();
                if (dataType!= undefined && dataType.toLowerCase()=='jscript') { xhrRespText = eval(xhrRespText);}
                if (xhr.status==200) { //请求成功并返回OK
                    eval(success(xhrRespText, xhr.status, xhr));
                    result.data = xhrRespText;
                } else {
                    eval(error(xhr.status, xhr));
                }
                eval(complete(xhr, xhr.status));
                clearTimeout(ajaxTimeId);
            }
        }
        xhr.send((type.toLowerCase()=='post' && data!=undefined) ? data : null);
    };

    if (context!=undefined && Object.prototype.toString.call(context).indexOf('[object HTML')!=-1) result.context = context;
    else result.context = document.body;

    return result;
};
